package com.example.medicalrecord.AggregationModule.registered.service.impl;

import com.example.medicalrecord.AggregationModule.patient.mapper.PatientMapper;
import com.example.medicalrecord.AggregationModule.patient.pojo.dto.PatientQuery;
import com.example.medicalrecord.AggregationModule.patient.pojo.vo.PatientVO;
import com.example.medicalrecord.AggregationModule.registered.mapper.RegisteredMapper;
import com.example.medicalrecord.AggregationModule.registered.pojo.dto.RegisteredQuery;
import com.example.medicalrecord.AggregationModule.registered.pojo.vo.RegisteredVo;
import com.example.medicalrecord.AggregationModule.registered.service.RegisteredService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

@Slf4j
@Service
public class RegisteredServiceImpl implements RegisteredService {
    @Autowired
    RegisteredMapper registeredMapper;
    @Autowired
    PatientMapper patientMapper;
    @Override
    public List<RegisteredVo> selectByDoctorId(RegisteredQuery registeredQuery) {
        log.debug("业务层查询挂号单，添加患者姓名");
        //创建患者查询类
        PatientQuery patientQuery = new PatientQuery();
        if (registeredQuery.getPatientName()!=null){
            //查询患者名字，获取患者集合
            patientQuery.setPatientName(registeredQuery.getPatientName());
            List<PatientVO> patList =patientMapper.select(patientQuery);
            //创建一个患者id集合
            List<Long> idList = new ArrayList<>();
            //循环获取患者id，添加到id集合中
            for (PatientVO patientVo : patList) {
              // RegisteredVo registeredVo= registeredMapper.selectByPatientId(patientVo.getPatientId);
              // regList.add(registeredVo);
                //将患者id加入患者id集合
                idList.add(patientVo.getId());
            }
            { }
            //将患者id集合注入到regquery中
            registeredQuery.setPatientIdList(idList);

        }else {
            //如果没有名字，就没用使用查询挂号管理表单，需要增加系统时间
            //校验日期数据，设置当前日期
            DateValidate(registeredQuery);
            }

        List<RegisteredVo> list = registeredMapper.select(registeredQuery);
        //查询患者id，给挂号单注入患者姓名
        for (RegisteredVo registeredVo : list) {
            patientQuery=new PatientQuery();
            //给患者查询类注入id
            //patientQuery.setPatientId(registeredVo.getPatientId());
            //查询患者通过id，获取返回响应类
            PatientVO patientVo=patientMapper.selectByPatientId(registeredVo.getPatientId());
            //将患者的名字注入到挂号单响应类
            registeredVo.setPatientName(patientVo.getPatientName());
        }
        return list;
    }

    public void DateValidate(RegisteredQuery registeredQuery){
        //如果日期为空，生成一个当天的零点时间，查询挂单号预约时间为当天的记录
        Date time = registeredQuery.getMakeTime();
        if (time==null){
            //创建一个日历类，获取当天零点时间
            Calendar calendar = Calendar.getInstance();
            calendar.set(Calendar.HOUR_OF_DAY,0);
            calendar.set(Calendar.MINUTE,0);
            calendar.set(Calendar.SECOND,0);
            //将系统时间添加进查询类
            registeredQuery.setMakeTime(calendar.getTime());
        }

    }
}
